package com.ukefu.util.es;

import static org.elasticsearch.index.query.QueryBuilders.rangeQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;

import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaBuilder.In;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.index.query.QueryStringQueryBuilder.Operator;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.metrics.cardinality.InternalCardinality;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.Specification;

import com.ukefu.core.UKDataContext;
import com.ukefu.util.UKTools;
import com.ukefu.util.WebIMReport;
import com.ukefu.webim.service.es.EkmKnowledgeMasterRepository;
import com.ukefu.webim.service.es.EkmKnowledgeTimesRepository;
import com.ukefu.webim.service.es.VoiceTranscriptionRepository;
import com.ukefu.webim.service.es.WorkOrdersRepository;
import com.ukefu.webim.service.impl.ESDataExchangeImpl;
import com.ukefu.webim.service.repository.AgentServiceRepository;
import com.ukefu.webim.service.repository.StatusEventRepository;
import com.ukefu.webim.web.model.AgentService;
import com.ukefu.webim.web.model.EkmDimension;
import com.ukefu.webim.web.model.EkmKnowledgeMaster;
import com.ukefu.webim.web.model.EkmKnowledgeTimes;
import com.ukefu.webim.web.model.FormFilter;
import com.ukefu.webim.web.model.FormFilterItem;
import com.ukefu.webim.web.model.JobDetail;
import com.ukefu.webim.web.model.MetadataTable;
import com.ukefu.webim.web.model.QualityFormFilterItem;
import com.ukefu.webim.web.model.StatusEvent;
import com.ukefu.webim.web.model.User;
import com.ukefu.webim.web.model.VoiceTranscription;
import com.ukefu.webim.web.model.WorkOrders;

public class SearchTools {
	
	public static PageImpl<UKDataBean> search(String orgi , FormFilter formFilter , List<FormFilterItem> itemList , MetadataTable metadataTable , boolean loadRef , int p, int ps){
		return search(query(orgi, formFilter, itemList, metadataTable, loadRef, p, ps), metadataTable, loadRef, p, ps);
	}
	
	public static PageImpl<UKDataBean> dissearch(String orgi , FormFilter formFilter , List<FormFilterItem> itemList , MetadataTable metadataTable , int p, int ps){
		BoolQueryBuilder queryBuilder = query(orgi, formFilter, itemList, metadataTable, false, p, ps) ;
		queryBuilder.must(termQuery("status", UKDataContext.NamesDisStatusType.NOT.toString())) ;
		queryBuilder.must(termQuery("validresult", "valid")) ;
		return search(queryBuilder, metadataTable, false, p, ps);
	}
	
	public static PageImpl<UKDataBean> recoversearch(String orgi , String cmd ,String id, MetadataTable metadataTable , int p, int ps){
		BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
		queryBuilder.must(termQuery("orgi", orgi)) ;
		queryBuilder.mustNot(termQuery("status", UKDataContext.NamesDisStatusType.NOT.toString())) ;
		queryBuilder.must(termQuery("validresult", "valid")) ;
		
		switch(cmd) {
			//case "actid" : queryBuilder.must(termQuery("actid", id)) ; break ;
			case "batid" : queryBuilder.must(termQuery("batid", id)) ; 
					BoolQueryBuilder queryBuilder1 = new BoolQueryBuilder();
					queryBuilder1.should(termQuery("callstatus", UKDataContext.NameStatusTypeEnum.NOTCALL.toString()));
					queryBuilder1.should(termQuery("callstatus", UKDataContext.NameStatusTypeEnum.WASTE.toString()));
					queryBuilder.must(queryBuilder1) ;break ;
			case "batsuccess" :queryBuilder.must(termQuery("batid", id)) ; 
								queryBuilder.must(termQuery("callstatus", UKDataContext.NamesCalledEnum.SUCCESS.toString()));break ;
			case "batfailed" : queryBuilder.must(termQuery("batid", id)) ; 
								queryBuilder.must(termQuery("callstatus", UKDataContext.NamesCalledEnum.FAILD.toString()));break ;
			case "baterror" : queryBuilder.must(termQuery("batid", id)) ; 
								queryBuilder.must(termQuery("callstatus", UKDataContext.NamesCalledEnum.ERROR.toString()));break ;
			//case "taskid" : queryBuilder.must(termQuery("taskid", id)) ; break ;
			//case "filterid" : queryBuilder.must(termQuery("filterid", id)) ; break ;
			case "agent" : queryBuilder.must(termQuery(UKDataContext.UKEFU_SYSTEM_DIS_AGENT, id)) ;
					BoolQueryBuilder queryBuilder3 = new BoolQueryBuilder();
					queryBuilder3.should(termQuery("callstatus", UKDataContext.NameStatusTypeEnum.NOTCALL.toString()));
					queryBuilder3.should(termQuery("callstatus", UKDataContext.NameStatusTypeEnum.WASTE.toString()));
					queryBuilder.must(queryBuilder3) ;break ;
			case "agentsuccess" : queryBuilder.must(termQuery(UKDataContext.UKEFU_SYSTEM_DIS_AGENT, id)) ; 
								queryBuilder.must(termQuery("callstatus", UKDataContext.NamesCalledEnum.SUCCESS.toString()));break ;
			case "agentfailed" : queryBuilder.must(termQuery(UKDataContext.UKEFU_SYSTEM_DIS_AGENT, id)) ; 
								queryBuilder.must(termQuery("callstatus", UKDataContext.NamesCalledEnum.FAILD.toString()));break ;
			case "agenterror" : queryBuilder.must(termQuery(UKDataContext.UKEFU_SYSTEM_DIS_AGENT, id)) ; 
								queryBuilder.must(termQuery("callstatus", UKDataContext.NamesCalledEnum.ERROR.toString()));break ;
			case "forecast" : queryBuilder.must(termQuery(UKDataContext.UKEFU_SYSTEM_DIS_FORECAST, id)) ;
					BoolQueryBuilder queryBuilder2 = new BoolQueryBuilder();
					queryBuilder2.should(termQuery("callstatus", UKDataContext.NameStatusTypeEnum.NOTCALL.toString()));
					queryBuilder2.should(termQuery("callstatus", UKDataContext.NameStatusTypeEnum.WASTE.toString()));
					queryBuilder.must(queryBuilder2) ;break ;
			case "forecastsuccess" : queryBuilder.must(termQuery(UKDataContext.UKEFU_SYSTEM_DIS_FORECAST, id)) ; 
							queryBuilder.must(termQuery("callstatus", UKDataContext.NamesCalledEnum.SUCCESS.toString()));break ;
			case "forecastfailed" : queryBuilder.must(termQuery(UKDataContext.UKEFU_SYSTEM_DIS_FORECAST, id)) ; 
							queryBuilder.must(termQuery("callstatus", UKDataContext.NamesCalledEnum.FAILD.toString()));break ;
			case "forecasterror" : queryBuilder.must(termQuery(UKDataContext.UKEFU_SYSTEM_DIS_FORECAST, id)) ; 
							queryBuilder.must(termQuery("callstatus", UKDataContext.NamesCalledEnum.ERROR.toString()));break ;
			case "ai" : queryBuilder.must(termQuery(UKDataContext.UKEFU_SYSTEM_DIS_AI, id)) ;
					BoolQueryBuilder queryBuilder4 = new BoolQueryBuilder();
					queryBuilder4.should(termQuery("callstatus", UKDataContext.NameStatusTypeEnum.NOTCALL.toString()));
					queryBuilder4.should(termQuery("callstatus", UKDataContext.NameStatusTypeEnum.WASTE.toString()));
					queryBuilder.must(queryBuilder4) ;break ;
			case "aisuccess" : queryBuilder.must(termQuery(UKDataContext.UKEFU_SYSTEM_DIS_AI, id)) ; 
								queryBuilder.must(termQuery("callstatus", UKDataContext.NamesCalledEnum.SUCCESS.toString()));break ;
			case "aiincall" : queryBuilder.must(termQuery(UKDataContext.UKEFU_SYSTEM_DIS_AI, id)) ; 
								queryBuilder.must(termQuery("callstatus", UKDataContext.NamesCalledEnum.CALLING.toString()));break ;
			case "aifailed" : queryBuilder.must(termQuery(UKDataContext.UKEFU_SYSTEM_DIS_AI, id)) ;
							queryBuilder.must(termQuery("callstatus", UKDataContext.NamesCalledEnum.FAILD.toString()));break ;
			case "aierror" : queryBuilder.must(termQuery(UKDataContext.UKEFU_SYSTEM_DIS_AI, id)) ;
						queryBuilder.must(termQuery("callstatus", UKDataContext.NamesCalledEnum.ERROR.toString()));break ;
			case "skill" : queryBuilder.must(termQuery(UKDataContext.UKEFU_SYSTEM_DIS_ORGAN, id)) ; 
					BoolQueryBuilder queryBuilder5 = new BoolQueryBuilder();
					queryBuilder5.should(termQuery("callstatus", UKDataContext.NameStatusTypeEnum.NOTCALL.toString()));
					queryBuilder5.should(termQuery("callstatus", UKDataContext.NameStatusTypeEnum.WASTE.toString()));
					queryBuilder.must(queryBuilder5) ;break ;
			case "skillnot" : queryBuilder.must(termQuery(UKDataContext.UKEFU_SYSTEM_DIS_ORGAN, id)) ; //未分配
					queryBuilder.should(termQuery("status", UKDataContext.NamesDisStatusType.NOT.toString()));break ;
			case "skillsuccess" : queryBuilder.must(termQuery(UKDataContext.UKEFU_SYSTEM_DIS_ORGAN, id)) ; 
						queryBuilder.must(termQuery("callstatus", UKDataContext.NamesCalledEnum.SUCCESS.toString()));break ;
			case "skillfailed" : queryBuilder.must(termQuery(UKDataContext.UKEFU_SYSTEM_DIS_ORGAN, id)) ;
						queryBuilder.must(termQuery("callstatus", UKDataContext.NamesCalledEnum.FAILD.toString()));break ;
			case "skillerror" : queryBuilder.must(termQuery(UKDataContext.UKEFU_SYSTEM_DIS_ORGAN, id)) ;
						queryBuilder.must(termQuery("callstatus", UKDataContext.NamesCalledEnum.ERROR.toString()));break ;
			//case "taskskill" : queryBuilder.must(termQuery("taskid", id)).must(termQuery("status", UKDataContext.NamesDisStatusType.DISAGENT.toString())) ; break ;
			//case "filterskill" : queryBuilder.must(termQuery("filterid", id)).must(termQuery("status", UKDataContext.NamesDisStatusType.DISAGENT.toString())) ; break ;
			default : queryBuilder.must(termQuery("actid", "NOT_EXIST_KEY")) ;  //必须传入一个进来;
		}
		
		return search(queryBuilder, metadataTable, false, p, ps);
	}
	/**
	 * 
	 * @param orgi
	 * @param agent
	 * @param p
	 * @param ps
	 * @return
	 */
	public static PageImpl<UKDataBean> agentsearch(String orgi ,boolean excludeCalled ,  String agent , int p, int ps){
		BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
		queryBuilder.must(termQuery("orgi", orgi)) ;
		if(excludeCalled){
			queryBuilder.must(termQuery("callstatus", UKDataContext.NameStatusTypeEnum.NOTCALL.toString())) ;
		}
		queryBuilder.must(termQuery("validresult", "valid")) ;
		queryBuilder.must(termQuery(UKDataContext.UKEFU_SYSTEM_DIS_AGENT, agent)) ;
		queryBuilder.must(termQuery("status", UKDataContext.NamesDisStatusType.DISAGENT.toString())) ;
		
		return search(queryBuilder, p, ps);
	}
	
	/**
	 * 
	 * @param orgi
	 * @param agent
	 * @param p
	 * @param ps
	 * @return
	 */
	public static PageImpl<UKDataBean> agentapsearch(String orgi , String agent , int p, int ps){
		BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
		queryBuilder.must(termQuery("orgi", orgi)) ;
		queryBuilder.must(termQuery("validresult", "valid")) ;
		queryBuilder.must(termQuery(UKDataContext.UKEFU_SYSTEM_DIS_AGENT, agent)) ;
		queryBuilder.must(termQuery("apstatus", true)) ;		//预约状态
		
		queryBuilder.must(rangeQuery("aptime").to(System.currentTimeMillis())) ;		//预约状态
		
		return search(queryBuilder, p, ps);
	}
	
	/**
	 * 
	 * @param orgi
	 * @param agent
	 * @param p
	 * @param ps
	 * @return
	 */
	public static PageImpl<UKDataBean> aisearch(String orgi , int p, int ps){
		BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
		queryBuilder.must(termQuery("orgi", orgi)) ;
		queryBuilder.must(termQuery("callstatus", UKDataContext.NameStatusTypeEnum.NOTCALL.toString())) ;
		
		queryBuilder.must(termQuery("validresult", "valid")) ;
		queryBuilder.must(termQuery("status", UKDataContext.NamesDisStatusType.DISAI.toString())) ;
		
		return search(queryBuilder, p, ps);
	}
	/**
	 * 通过ai id 查询
	 * @param orgi
	 * @param agent
	 * @param p
	 * @param ps
	 * @return
	 */
	public static PageImpl<UKDataBean> aisearch(String orgi ,String aiid, int p, int ps ,JobDetail jobDetail){
		BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
		queryBuilder.must(termQuery("orgi", orgi)) ;
		queryBuilder.must(termQuery("callstatus", UKDataContext.NameStatusTypeEnum.NOTCALL.toString())) ;
		
		queryBuilder.must(termQuery("validresult", "valid")) ;
		queryBuilder.must(termQuery("status", UKDataContext.NamesDisStatusType.DISAI.toString())) ;
		queryBuilder.must(termQuery(UKDataContext.UKEFU_SYSTEM_DIS_AI,aiid));
		
		if(jobDetail != null && !StringUtils.isBlank(jobDetail.getBatid())) {
			queryBuilder.must(termQuery("batid", jobDetail.getBatid())) ;
		}
		
		return search(queryBuilder, p, ps);
	}
	
	/**
	 * 通过ai id 查询
	 * @param orgi
	 * @param agent
	 * @param p
	 * @param ps
	 * @return
	 */
	public static PageImpl<UKDataBean> forecastsearch(String orgi ,String queueid, int p, int ps){
		BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
		queryBuilder.must(termQuery("orgi", orgi)) ;
		queryBuilder.must(termQuery("callstatus", UKDataContext.NameStatusTypeEnum.NOTCALL.toString())) ;
		
		queryBuilder.must(termQuery("validresult", "valid")) ;
		queryBuilder.must(termQuery("status", UKDataContext.NamesDisStatusType.DISFORECAST.toString())) ;
		queryBuilder.must(termQuery(UKDataContext.UKEFU_SYSTEM_DIS_FORECAST,queueid));
		return search(queryBuilder, p, ps);
	}
	/**
	 * 
	 * @param orgi
	 * @param agent
	 * @param p
	 * @param ps
	 * @return
	 */
	public static PageImpl<UKDataBean> ainamesearch(String orgi , String phonenum){
		BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
		queryBuilder.must(termQuery("orgi", orgi)) ;
		queryBuilder.must(termQuery("validresult", "valid")) ;
		queryBuilder.must(termQuery("status", UKDataContext.NamesDisStatusType.DISAI.toString())) ;
		StringBuffer strb = new StringBuffer();
		if(!StringUtils.isBlank(phonenum)) {
			if(phonenum.startsWith("0")) {
				strb.append(phonenum.substring(1)) ;
			}else {
				strb.append(phonenum) ;
			}
		}else {
			strb.append(UKDataContext.UKEFU_SYSTEM_NO_DAT) ;
		}
		queryBuilder.must(new QueryStringQueryBuilder(strb.toString()).defaultOperator(Operator.OR) );
		return search(queryBuilder,0, 1);
	}

	/**
	 * 
	 * @param orgi
	 * @param agent
	 * @param p
	 * @param ps
	 * @return
	 */
	public static PageImpl<UKDataBean> aiidsearch(String orgi , String id){
		BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
		queryBuilder.must(termQuery("orgi", orgi)) ;
		queryBuilder.must(termQuery("validresult", "valid")) ;
		queryBuilder.must(termQuery("status", UKDataContext.NamesDisStatusType.DISAI.toString())) ;
		StringBuffer strb = new StringBuffer();
		if(!StringUtils.isBlank(id)) {
			strb.append(id) ;
		}else {
			strb.append(UKDataContext.UKEFU_SYSTEM_NO_DAT) ;
		}
		queryBuilder.must(termQuery("id",strb.toString())) ;
		return search(queryBuilder,0, 1);
	}
	/**
	 * 
	 * @param orgi
	 * @param agent
	 * @param p
	 * @param ps
	 * @return
	 */
	public static PageImpl<UKDataBean> namesearch(String orgi , String phonenum){
		return namesearch(orgi , phonenum , true);
	}
	
	/**
	 * 
	 * @param orgi
	 * @param agent
	 * @param p
	 * @param ps
	 * @return
	 */
	public static PageImpl<UKDataBean> namesearch(String orgi , String phonenum , boolean valid){
		BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
		queryBuilder.must(termQuery("orgi", orgi)) ;
		if(valid == true) {
			queryBuilder.must(termQuery("validresult", "valid")) ;
		}
		queryBuilder.must(termQuery("status", UKDataContext.NamesDisStatusType.DISAGENT.toString())) ;
		StringBuffer strb = new StringBuffer();
		if(!StringUtils.isBlank(phonenum)) {
			strb.append(phonenum) ;
			if(phonenum.startsWith("0")) {
				strb.append(" ").append(phonenum.substring(1)) ;
			}
		}else {
			strb.append(UKDataContext.UKEFU_SYSTEM_NO_DAT) ;
		}
		queryBuilder.must(new QueryStringQueryBuilder(strb.toString()).defaultOperator(Operator.OR) );
		return search(queryBuilder,0, 1);
	}
	
	/**
	 * 
	 * @param orgi
	 * @param agent
	 * @param p
	 * @param ps
	 * @return
	 */
	public static PageImpl<UKDataBean> search(BoolQueryBuilder queryBuilder, int p, int ps){
		return search(queryBuilder, null, true, p, ps);
	}
	/**
	 * 
	 * @param orgi
	 * @param agent
	 * @param p
	 * @param ps
	 * @return
	 */
	public static PageImpl<UKDataBean> search(BoolQueryBuilder queryBuilder, int p, int ps , String sort){
		return search(queryBuilder, null, true, p, ps , sort);
	}
	/**
	 * 
	 * @param queryBuilder
	 * @param metadataTable
	 * @param loadRef
	 * @param p
	 * @param ps
	 * @return
	 */
	public static PageImpl<UKDataBean> search(BoolQueryBuilder queryBuilder , MetadataTable metadataTable , boolean loadRef , int p, int ps){
		return search(queryBuilder, metadataTable, loadRef, p, ps, "createtime");
	}
	
	/**
	 * 
	 * @param queryBuilder
	 * @param metadataTable
	 * @param loadRef
	 * @param p
	 * @param ps
	 * @return
	 */
	private static PageImpl<UKDataBean> search(BoolQueryBuilder queryBuilder , MetadataTable metadataTable , boolean loadRef , int p, int ps , String sort){
		ESDataExchangeImpl esDataExchange = UKDataContext.getContext().getBean(ESDataExchangeImpl.class);
		return esDataExchange.findPageResult(queryBuilder, UKDataContext.CALLOUT_INDEX, metadataTable, new PageRequest(p, ps , Sort.Direction.ASC, sort) , loadRef) ;
	}
	
	/**
	 * 
	 * @param queryBuilder
	 * @param metadataTable
	 * @param loadRef
	 * @param p
	 * @param ps
	 * @return
	 */
	public static PageImpl<UKDataBean> aggregation(BoolQueryBuilder queryBuilder , String aggField, boolean loadRef , int p, int ps){
		ESDataExchangeImpl esDataExchange = UKDataContext.getContext().getBean(ESDataExchangeImpl.class);
		return esDataExchange.findAllPageAggResult(queryBuilder , aggField ,  new PageRequest(p, ps , Sort.Direction.ASC, "createtime") , loadRef , null) ;
	}
	
	/**
	 * 
	 * @param queryBuilder
	 * @param metadataTable
	 * @param loadRef
	 * @param p
	 * @param ps
	 * @return
	 */
	public static UKDataBean get(UKDataBean dataBean){
		ESDataExchangeImpl esDataExchange = UKDataContext.getContext().getBean(ESDataExchangeImpl.class);
		return esDataExchange.getIObjectByPK(dataBean, dataBean.getId());
	}
	
	/**
	 * 
	 * @param queryBuilder
	 * @param metadataTable
	 * @param loadRef
	 * @param p
	 * @param ps
	 * @return
	 */
	public static UKDataBean get(String type, String id){
		ESDataExchangeImpl esDataExchange = UKDataContext.getContext().getBean(ESDataExchangeImpl.class);
		return esDataExchange.getIObjectByPK(type, id);
	}
	
	/**
	 * 
	 * @param queryBuilder
	 * @param metadataTable
	 * @param loadRef
	 * @param p
	 * @param ps
	 * @return
	 */
	public static void save(UKDataBean dataBean){
		ESDataExchangeImpl esDataExchange = UKDataContext.getContext().getBean(ESDataExchangeImpl.class);
		try {
			esDataExchange.saveIObject(dataBean);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void saveAndRefresh(UKDataBean dataBean,boolean refresh){
		ESDataExchangeImpl esDataExchange = UKDataContext.getContext().getBean(ESDataExchangeImpl.class);
		try {
			esDataExchange.saveIObject(dataBean,refresh);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void updateAndRefresh(UKDataBean dataBean,boolean refresh ){
		ESDataExchangeImpl esDataExchange = UKDataContext.getContext().getBean(ESDataExchangeImpl.class);
		try {
			esDataExchange.updatePartObject(dataBean,refresh);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * EKM 知识库首页 - 数据聚合
	 */
	public static PageImpl<EkmDataBean> aggregationEkm(BoolQueryBuilder queryBuilder , boolean loadRef , int p, int ps){
		ESDataExchangeImpl esDataExchange = UKDataContext.getContext().getBean(ESDataExchangeImpl.class);
		return esDataExchange.findAllPageAggResultEkm(queryBuilder ,   new PageRequest(p, ps , Sort.Direction.ASC, "createtime") , loadRef , "uk_ekm_kb_master") ;
	}
	
	/**
	 * EKM 知识库维度
	 */
	public static List<EkmDataBean> sumEkmDimension(BoolQueryBuilder queryBuilder ,String orgi,User user, List<EkmDimension> ekmDimensionList){
		
		EkmKnowledgeMasterRepository ekmKnowledgeEsRes = UKDataContext.getContext().getBean(EkmKnowledgeMasterRepository.class) ;
		EkmKnowledgeTimesRepository ekmKnowledgeTimesRes = UKDataContext.getContext().getBean(EkmKnowledgeTimesRepository.class) ;
		List<EkmDataBean> dataBeanList = new ArrayList<EkmDataBean>() ;
		if (ekmDimensionList!=null && ekmDimensionList.size()>0) {
			for(EkmDimension dimension : ekmDimensionList){
				int collectResult = 0 ;
				int viewnumResult = 0 ;
				BoolQueryBuilder boolQueryBuild = QueryBuilders.boolQuery();
				boolQueryBuild.must(QueryBuilders.wildcardQuery("dimenid", "*"+dimension.getId()+"*"));
				List<EkmKnowledgeMaster> ekmKnowledgeList = ekmKnowledgeEsRes.findByDatastatusAndOrgi(boolQueryBuild,false,orgi) ;
				if (ekmKnowledgeList!=null && ekmKnowledgeList.size()>0) {
					for(EkmKnowledgeMaster knowledgeMaster : ekmKnowledgeList){
						List<EkmKnowledgeTimes> ekmKnowledgeTimesList = ekmKnowledgeTimesRes.findByKbidAndOrgi(knowledgeMaster.getId(), orgi) ;
						if (ekmKnowledgeTimesList!=null && ekmKnowledgeTimesList.size()>0) {
							collectResult = ekmKnowledgeTimesList.get(0)!=null?collectResult+ekmKnowledgeTimesList.get(0).getCollectimes():0 ;
							viewnumResult = ekmKnowledgeTimesList.get(0)!=null?viewnumResult+ekmKnowledgeTimesList.get(0).getViewtimes():0;
						}
					}
				}
				dimension.setCollectnum(collectResult);
				dimension.setViewnum(viewnumResult);
				EkmDataBean ekmDataBean = new EkmDataBean() ;
				ekmDataBean.setEkmdimension(dimension);
				ekmDataBean.setType("dimenid");
				dataBeanList.add(ekmDataBean) ;
			}
		}
		return dataBeanList;
	}
	/**
	 * 	查询参与质检的通话记录
	 * @param orgi
	 * @param qcFormFilterItemList
	 * @return
	 */
	public static List<StatusEvent> searchQualityStatusEvent(final String orgi, final List<QualityFormFilterItem> qcFormFilterItemList, User user){
		StatusEventRepository statusEventRes = UKDataContext.getContext().getBean(StatusEventRepository.class);
		final VoiceTranscriptionRepository voiceTranscriptionRes = UKDataContext.getContext().getBean(VoiceTranscriptionRepository.class);
		Page<StatusEvent> page = statusEventRes.findAll(new Specification<StatusEvent>(){
			@Override
			public Predicate toPredicate(Root<StatusEvent> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();  //and关系
				Map<String, List<Predicate>> orMap = new HashMap<String, List<Predicate>>();
				list.add(cb.isNotNull(root.get("userid").as(String.class))) ;
				list.add(cb.equal(root.get("orgi").as(String.class), orgi)) ;
				list.add(cb.equal(root.get("record").as(boolean.class), true)) ;
				list.add(cb.equal(root.get("misscall").as(boolean.class), false)) ;
				list.add(cb.greaterThan(root.get("duration").as(int.class), 0)) ;
				list.add(cb.and(cb.or(cb.and(root.get("qualitydistype").as(String.class).isNull()),cb.equal(root.get("qualitydistype").as(String.class), UKDataContext.QualityDisStatusType.NOT.toString())))) ;
				
				if(qcFormFilterItemList.size() > 0 && qcFormFilterItemList.get(0).getField() != null) {
					try {
						for(QualityFormFilterItem formFilterItem : qcFormFilterItemList) {
							List<Predicate> inlist = orMap.get(formFilterItem.getField())==null ? new ArrayList<Predicate>() : orMap.get(formFilterItem.getField());
							if(formFilterItem.getField().equals("q")) {
								if(!"*".equals(formFilterItem.getValue())) {
									list.add(cb.or(cb.like(root.get("discaller").as(String.class), "%" + formFilterItem.getValue() + "%")
											,cb.like(root.get("discalled").as(String.class), "%" + formFilterItem.getValue() + "%"))) ;
								}
							}else if(formFilterItem.getField().equals("content")){
								BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
								boolQueryBuilder.must(QueryBuilders.boolQuery().must(new QueryStringQueryBuilder(formFilterItem.getValue()).defaultOperator(Operator.AND))) ;
								boolQueryBuilder.must(QueryBuilders.termQuery("orgi", orgi)) ;
								Page<VoiceTranscription> voiceTranscriptionList = voiceTranscriptionRes.findByOrgi(boolQueryBuilder, new PageRequest(0, 10000));
								In<Object> in = cb.in(root.get("id"));
								if(!voiceTranscriptionList.getContent().isEmpty()){
									if("AND".equals(formFilterItem.getComp())) {
										for(VoiceTranscription voiceTranscription : voiceTranscriptionList.getContent()){
											in.value(voiceTranscription.getCallid()) ;
										}
										list.add(in) ;
									}else if("OR".equals(formFilterItem.getComp())) {
										for(VoiceTranscription voiceTranscription : voiceTranscriptionList.getContent()){
											in.value(voiceTranscription.getCallid()) ;
										}
										inlist = orMap.get("id")==null ? new ArrayList<Predicate>() : orMap.get("id");
										inlist.add(in) ;
										orMap.put("id", inlist);
									}
								}else{
									if("AND".equals(formFilterItem.getComp())) {
										in.value(UKDataContext.UKEFU_SYSTEM_NO_DAT) ;
										list.add(in) ;
									}else if("OR".equals(formFilterItem.getComp())) {
										in.value(UKDataContext.UKEFU_SYSTEM_NO_DAT) ;
										inlist = orMap.get("id")==null ? new ArrayList<Predicate>() : orMap.get("id");
										inlist.add(in) ;
										orMap.put("id", inlist);
									}
								}
							}else if(isDate(formFilterItem.getValue(), UKTools.dateFormate)){
								String field = changField(formFilterItem.getField());
								switch(formFilterItem.getCond()) {
									case "01" : 
										if("AND".equals(formFilterItem.getComp())) {
											list.add(cb.greaterThan(root.get(field).as(Date.class), UKTools.dateFormate.parse(formFilterItem.getValue()))) ;
										}else if("OR".equals(formFilterItem.getComp())) {
											inlist.add(cb.greaterThan(root.get(field).as(Date.class), UKTools.dateFormate.parse(formFilterItem.getValue()))) ;
										}
										break ;
									case "02" : 
										if("AND".equals(formFilterItem.getComp())) {
											list.add(cb.greaterThanOrEqualTo(root.get(field).as(Date.class), UKTools.dateFormate.parse(formFilterItem.getValue()))) ;
										}else if("OR".equals(formFilterItem.getComp())) {
											inlist.add(cb.greaterThanOrEqualTo(root.get(field).as(Date.class), UKTools.dateFormate.parse(formFilterItem.getValue()))) ;
										}
										break ;
									case "03" : 
										if("AND".equals(formFilterItem.getComp())) {
											list.add(cb.lessThan(root.get(field).as(Date.class), UKTools.dateFormate.parse(formFilterItem.getValue()))) ;
										}else if("OR".equals(formFilterItem.getComp())) {
											inlist.add(cb.lessThan(root.get(field).as(Date.class), UKTools.dateFormate.parse(formFilterItem.getValue()))) ;
										}
										break ;
									case "04" : 
										if("AND".equals(formFilterItem.getComp())) {
											list.add(cb.lessThanOrEqualTo(root.get(field).as(Date.class), UKTools.dateFormate.parse(formFilterItem.getValue()))) ;
										}else if("OR".equals(formFilterItem.getComp())) {
											inlist.add(cb.lessThanOrEqualTo(root.get(field).as(Date.class), UKTools.dateFormate.parse(formFilterItem.getValue()))) ;
										}
										break ;
									case "05" : 
										if("AND".equals(formFilterItem.getComp())) {
											list.add(cb.equal(root.get(field).as(Date.class), UKTools.dateFormate.parse(formFilterItem.getValue()))) ;
										}else if("OR".equals(formFilterItem.getComp())) {
											inlist.add(cb.equal(root.get(field).as(Date.class), UKTools.dateFormate.parse(formFilterItem.getValue()))) ;
										}
										break ;
									case "06" : 
										if("AND".equals(formFilterItem.getComp())) {
											list.add(cb.notEqual(root.get(field).as(Date.class), UKTools.dateFormate.parse(formFilterItem.getValue()))) ;
										}else if("OR".equals(formFilterItem.getComp())) {
											inlist.add(cb.notEqual(root.get(field).as(Date.class), UKTools.dateFormate.parse(formFilterItem.getValue()))) ;
										}
										break ;
									default :
										break ;
								}
								orMap.put(formFilterItem.getField(), inlist);
							}else {
								Number number = null ;
								String field = changField(formFilterItem.getField());
								if(!StringUtils.isBlank(formFilterItem.getValue()) && formFilterItem.getValue().matches("[+-.]{0,1}[\\d.]{1,}")) {
									number = NumberFormat.getInstance().parse(formFilterItem.getValue()) ;
								}
								if(number == null){
									number = 0;
								}
								switch(formFilterItem.getCond()) {
									case "01" : 
										if("AND".equals(formFilterItem.getComp())) {
											list.add(cb.gt(root.get(field).as(Number.class), number)) ;
										}else if("OR".equals(formFilterItem.getComp())) {
											inlist.add(cb.gt(root.get(field).as(Number.class), number)) ;
										}
										break ;
									case "02" : 
										if("AND".equals(formFilterItem.getComp())) {
											list.add(cb.ge(root.get(field).as(Number.class), number)) ;
										}else if("OR".equals(formFilterItem.getComp())) {
											inlist.add(cb.ge(root.get(field).as(Number.class), number)) ;
										}
										break ;
									case "03" : 
										if("AND".equals(formFilterItem.getComp())) {
											list.add(cb.lt(root.get(field).as(Number.class), number)) ;
										}else if("OR".equals(formFilterItem.getComp())) {
											inlist.add(cb.lt(root.get(field).as(Number.class), number)) ;
										}
										break ;
									case "04" : 
										if("AND".equals(formFilterItem.getComp())) {
											list.add(cb.le(root.get(field).as(Number.class), number)) ;
										}else if("OR".equals(formFilterItem.getComp())) {
											inlist.add(cb.le(root.get(field).as(Number.class), number)) ;
										}
										break ;
									case "05" : 
										if("AND".equals(formFilterItem.getComp())) {
											list.add(cb.equal(root.get(field).as(String.class), formFilterItem.getValue())) ;
										}else if("OR".equals(formFilterItem.getComp())) {
											inlist.add(cb.equal(root.get(field).as(String.class), formFilterItem.getValue())) ;
										}
										break ;
									case "06" : 
										if("AND".equals(formFilterItem.getComp())) {
											list.add(cb.notEqual(root.get(field).as(String.class), formFilterItem.getValue())) ;
										}else if("OR".equals(formFilterItem.getComp())) {
											inlist.add(cb.notEqual(root.get(field).as(String.class), formFilterItem.getValue())) ;
										}
										break ;
									case "07" : 
										if("AND".equals(formFilterItem.getComp())) {
											list.add(cb.like(root.get(field).as(String.class), formFilterItem.getValue())) ;
										}else if("OR".equals(formFilterItem.getComp())) {
											inlist.add(cb.like(root.get(field).as(String.class), formFilterItem.getValue())) ;
										}
										break ;
									default :
										break ;
								}
								orMap.put(formFilterItem.getField(), inlist);
							}
						}
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				Predicate[] p = new Predicate[list.size()];  
				Predicate[] inp = null; 
				
				if (orMap != null && orMap.size() > 0) {
					for(String key : orMap.keySet()){
						if (orMap.get(key) != null) {
							List<Predicate> orList = orMap.get(key);
							if (orList != null && orList.size() > 0) {
								inp = new Predicate[orList.size()]; 
								list.add(cb.and(cb.or(orList.toArray(inp))));
							}
						}
					}
				}
				
				if(list.size() > 0){
					query.where(cb.and(list.toArray(p)));
				}
				return query.getRestriction(); 
			}}, new PageRequest(0, 10000 , Sort.Direction.DESC, "starttime")) ;
		return page.getContent();
	}
	/**
	 * 查询参与质检的工单
	 * @param orgi
	 * @param qcFormFilterItemList
	 * @return
	 */
	public static List<WorkOrders> searchQualityWorkOrders(String orgi, List<QualityFormFilterItem> qcFormFilterItemList, User user){
		
		WorkOrdersRepository workOrdersRes = UKDataContext.getContext().getBean(WorkOrdersRepository.class);
		//工单质检
		BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
		//筛选未分配质检的工单
		BoolQueryBuilder bool = new BoolQueryBuilder();
		bool.should(termQuery("qualitydistype",UKDataContext.QualityDisStatusType.DISAGENT.toString()));
		bool.should(termQuery("qualitydistype",UKDataContext.QualityDisStatusType.DISORGAN.toString()));
		boolQueryBuilder.mustNot(bool);
		
		//权限控制
		if(qcFormFilterItemList.size() > 0 && qcFormFilterItemList.get(0).getField() != null) {
			BoolQueryBuilder orBuilder = new BoolQueryBuilder();
			int orNums = 0 ;
			for(QualityFormFilterItem formFilterItem : qcFormFilterItemList) {
				QueryBuilder tempQueryBuilder = null ;
				if(formFilterItem.getField().equals("q")) {
					tempQueryBuilder = new QueryStringQueryBuilder(formFilterItem.getValue()).defaultOperator(Operator.AND) ;
				}else {
					String field = changField(formFilterItem.getField());
					switch(formFilterItem.getCond()) {
					case "01" : 
						tempQueryBuilder = rangeQuery(field).from(formFilterItem.getValue()).includeLower(false) ;
						break ;
					case "02" : 
						tempQueryBuilder = rangeQuery(field).from(formFilterItem.getValue()).includeLower(true) ;
						break ;
					case "03" : 
						tempQueryBuilder = rangeQuery(field).to(formFilterItem.getValue()).includeUpper(false) ;
						break ;
					case "04" : 
						tempQueryBuilder = rangeQuery(field).to(formFilterItem.getValue()).includeUpper(true) ;
						break ;
					case "05" : 
						tempQueryBuilder = termQuery(field , formFilterItem.getValue()) ;
						break ;
					case "06" : 
						tempQueryBuilder = termQuery(field , formFilterItem.getValue()) ;
						break ;
					case "07" : 
						tempQueryBuilder = new QueryStringQueryBuilder(formFilterItem.getValue()).field(field).defaultOperator(Operator.AND) ;
						break ;
					default :
						break ;
					}
				}
				if("AND".equalsIgnoreCase(formFilterItem.getComp())) {
					if("06".equals(formFilterItem.getCond())) {
						boolQueryBuilder.mustNot(tempQueryBuilder) ;
					}else {
						boolQueryBuilder.must(tempQueryBuilder) ;
					}
				}else {
					orNums ++ ;
					if("06".equals(formFilterItem.getCond())) {
						orBuilder.mustNot(tempQueryBuilder) ;
					}else {
						orBuilder.should(tempQueryBuilder) ;
					}
				}
			}
			if(orNums > 0) {
				boolQueryBuilder.must(orBuilder) ;
			}
		}
		Page<WorkOrders> page = workOrdersRes.findById(boolQueryBuilder, false, orgi,new PageRequest(0, 10000));
		return page.getContent();
	}
	/**
	 * 	查询参与质检的文字客服会话记录
	 * @param orgi
	 * @param qcFormFilterItemList
	 * @return
	 */
	public static List<AgentService> searchQualityAgentService(final String orgi, final List<QualityFormFilterItem> qcFormFilterItemList, User user){
		AgentServiceRepository agentServiceRes = UKDataContext.getContext().getBean(AgentServiceRepository.class);
		Page<AgentService> page = agentServiceRes.findAll(new Specification<AgentService>(){
			@Override
			public Predicate toPredicate(Root<AgentService> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();  //and关系
				List<Predicate> inlist = new ArrayList<Predicate>();  //or关系
				
				list.add(cb.equal(root.get("orgi").as(String.class), orgi)) ;
				list.add(cb.and(cb.or(cb.and(root.get("qualitydistype").as(String.class).isNull()),cb.equal(root.get("qualitydistype").as(String.class), UKDataContext.QualityDisStatusType.NOT.toString())))) ;
				
				if(qcFormFilterItemList.size() > 0 && qcFormFilterItemList.get(0).getField() != null) {
					try {
						for(QualityFormFilterItem formFilterItem : qcFormFilterItemList) {
							if(formFilterItem.getField().equals("q")) {
								list.add(cb.or(cb.like(root.get("agentusername").as(String.class), "%" + formFilterItem.getValue() + "%"),cb.like(root.get("username").as(String.class), "%" + formFilterItem.getValue() + "%"))) ;
							}else if(isDate(formFilterItem.getValue(), UKTools.dateFormate)){
								String field = changField(formFilterItem.getField());
								switch(formFilterItem.getCond()) {
									case "01" : 
										if("AND".equals(formFilterItem.getComp())) {
											list.add(cb.greaterThan(root.get(field).as(Date.class), UKTools.dateFormate.parse(formFilterItem.getValue()))) ;
										}else if("OR".equals(formFilterItem.getComp())) {
											inlist.add(cb.greaterThan(root.get(field).as(Date.class), UKTools.dateFormate.parse(formFilterItem.getValue()))) ;
										}
										break ;
									case "02" : 
										if("AND".equals(formFilterItem.getComp())) {
											list.add(cb.greaterThanOrEqualTo(root.get(field).as(Date.class), UKTools.dateFormate.parse(formFilterItem.getValue()))) ;
										}else if("OR".equals(formFilterItem.getComp())) {
											inlist.add(cb.greaterThanOrEqualTo(root.get(field).as(Date.class), UKTools.dateFormate.parse(formFilterItem.getValue()))) ;
										}
										break ;
									case "03" : 
										if("AND".equals(formFilterItem.getComp())) {
											list.add(cb.lessThan(root.get(field).as(Date.class), UKTools.dateFormate.parse(formFilterItem.getValue()))) ;
										}else if("OR".equals(formFilterItem.getComp())) {
											inlist.add(cb.lessThan(root.get(field).as(Date.class), UKTools.dateFormate.parse(formFilterItem.getValue()))) ;
										}
										break ;
									case "04" : 
										if("AND".equals(formFilterItem.getComp())) {
											list.add(cb.lessThanOrEqualTo(root.get(field).as(Date.class), UKTools.dateFormate.parse(formFilterItem.getValue()))) ;
										}else if("OR".equals(formFilterItem.getComp())) {
											inlist.add(cb.lessThanOrEqualTo(root.get(field).as(Date.class), UKTools.dateFormate.parse(formFilterItem.getValue()))) ;
										}
										break ;
									case "05" : 
										if("AND".equals(formFilterItem.getComp())) {
											list.add(cb.equal(root.get(field).as(Date.class), UKTools.dateFormate.parse(formFilterItem.getValue()))) ;
										}else if("OR".equals(formFilterItem.getComp())) {
											inlist.add(cb.equal(root.get(field).as(Date.class), UKTools.dateFormate.parse(formFilterItem.getValue()))) ;
										}
										break ;
									case "06" : 
										if("AND".equals(formFilterItem.getComp())) {
											list.add(cb.notEqual(root.get(field).as(Date.class), UKTools.dateFormate.parse(formFilterItem.getValue()))) ;
										}else if("OR".equals(formFilterItem.getComp())) {
											inlist.add(cb.notEqual(root.get(field).as(Date.class), UKTools.dateFormate.parse(formFilterItem.getValue()))) ;
										}
										break ;
									default :
										break ;
								}
							}else {
								Number number = null ;
								String field = changField(formFilterItem.getField());
								if(!StringUtils.isBlank(formFilterItem.getValue()) && formFilterItem.getValue().matches("[+-.]{0,1}[\\d.]{1,}")) {
									number = NumberFormat.getInstance().parse(formFilterItem.getValue()) ;
								}
								if(number == null){
									number = 0;
								}
								switch(formFilterItem.getCond()) {
								case "01" : 
									if("AND".equals(formFilterItem.getComp())) {
										list.add(cb.gt(root.get(field).as(Number.class), number)) ;
									}else if("OR".equals(formFilterItem.getComp())) {
										inlist.add(cb.gt(root.get(field).as(Number.class), number)) ;
									}
									break ;
								case "02" : 
									if("AND".equals(formFilterItem.getComp())) {
										list.add(cb.ge(root.get(field).as(Number.class), number)) ;
									}else if("OR".equals(formFilterItem.getComp())) {
										inlist.add(cb.ge(root.get(field).as(Number.class), number)) ;
									}
									break ;
								case "03" : 
									if("AND".equals(formFilterItem.getComp())) {
										list.add(cb.lt(root.get(field).as(Number.class), number)) ;
									}else if("OR".equals(formFilterItem.getComp())) {
										inlist.add(cb.lt(root.get(field).as(Number.class), number)) ;
									}
									break ;
								case "04" : 
									if("AND".equals(formFilterItem.getComp())) {
										list.add(cb.le(root.get(field).as(Number.class), number)) ;
									}else if("OR".equals(formFilterItem.getComp())) {
										inlist.add(cb.le(root.get((field)).as(Number.class), number)) ;
									}
									break ;
								case "05" : 
									if("AND".equals(formFilterItem.getComp())) {
										list.add(cb.equal(root.get(field).as(String.class), formFilterItem.getValue())) ;
									}else if("OR".equals(formFilterItem.getComp())) {
										inlist.add(cb.equal(root.get(field).as(String.class), formFilterItem.getValue())) ;
									}
									break ;
								case "06" : 
									if("AND".equals(formFilterItem.getComp())) {
										list.add(cb.notEqual(root.get(field).as(String.class), formFilterItem.getValue())) ;
									}else if("OR".equals(formFilterItem.getComp())) {
										inlist.add(cb.notEqual(root.get(field).as(String.class), formFilterItem.getValue())) ;
									}
									break ;
								case "07" : 
									if("AND".equals(formFilterItem.getComp())) {
										list.add(cb.like(root.get(field).as(String.class), formFilterItem.getValue())) ;
									}else if("OR".equals(formFilterItem.getComp())) {
										inlist.add(cb.like(root.get(field).as(String.class), formFilterItem.getValue())) ;
									}
									break ;
								default :
									break ;
							}
							}
						}
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				
				Predicate[] p = new Predicate[list.size()];  
				Predicate[] inp = new Predicate[inlist.size()];  
				
				if(inlist.size() > 0) {
					query.where(cb.and(cb.or(inlist.toArray(inp))));
				}
				if(list.size() > 0){
					query.where(cb.and(list.toArray(p)));
				}
				return query.getRestriction();   
			}}, new PageRequest(0, 10000 , Sort.Direction.DESC, "createtime")) ;
		return page.getContent();
	}
	public static BoolQueryBuilder query(String orgi , FormFilter formFilter , List<FormFilterItem> itemList , MetadataTable metadataTable , boolean loadRef , int p, int ps){
		BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
		queryBuilder.must(termQuery("orgi", orgi)) ;
		
		BoolQueryBuilder orBuilder = new BoolQueryBuilder();
		int orNums = 0 ;
		for(FormFilterItem formFilterItem : itemList) {
			QueryBuilder tempQueryBuilder = null ;
			if(formFilterItem.getField().equals("q")) {
				tempQueryBuilder = new QueryStringQueryBuilder(formFilterItem.getValue()).defaultOperator(Operator.AND) ;
			}else {
				switch(formFilterItem.getCond()) {
					case "01" : 
						tempQueryBuilder = rangeQuery(formFilterItem.getField()).from(formFilterItem.getValue()).includeLower(false) ;
						break ;
					case "02" : 
						tempQueryBuilder = rangeQuery(formFilterItem.getField()).from(formFilterItem.getValue()).includeLower(true) ;
						break ;
					case "03" : 
						tempQueryBuilder = rangeQuery(formFilterItem.getField()).to(formFilterItem.getValue()).includeUpper(false) ;
						break ;
					case "04" : 
						tempQueryBuilder = rangeQuery(formFilterItem.getField()).to(formFilterItem.getValue()).includeUpper(true) ;
						break ;
					case "05" : 
						tempQueryBuilder = termQuery(formFilterItem.getField() , formFilterItem.getValue()) ;
						break ;
					case "06" : 
						tempQueryBuilder = termQuery(formFilterItem.getField() , formFilterItem.getValue()) ;
						break ;
					case "07" : 
						tempQueryBuilder = new QueryStringQueryBuilder(formFilterItem.getValue()).field(formFilterItem.getField()).defaultOperator(Operator.AND) ;
						break ;
					default :
						break ;
				}
			}
			if("AND".equalsIgnoreCase(formFilterItem.getComp())) {
				if("06".equals(formFilterItem.getCond())) {
					queryBuilder.mustNot(tempQueryBuilder) ;
				}else {
					queryBuilder.must(tempQueryBuilder) ;
				}
			}else {
				orNums ++ ;
				if("06".equals(formFilterItem.getCond())) {
					orBuilder.mustNot(tempQueryBuilder) ;
				}else {
					orBuilder.should(tempQueryBuilder) ;
				}
			}
		}
		if(orNums > 0) {
			queryBuilder.must(orBuilder) ;
		}
		
		return queryBuilder;
	}
	
	public static PageImpl<UKDataBean> aggregationBatchData(BoolQueryBuilder queryBuilder , boolean loadRef , int p, int ps,String aggField){
		ESDataExchangeImpl esDataExchange = UKDataContext.getContext().getBean(ESDataExchangeImpl.class);
		return esDataExchange.findAllPageAggResultBatchData(queryBuilder ,   new PageRequest(p, ps , Sort.Direction.ASC, "createtime") , loadRef , null ,aggField) ;
	}
	
	public static String changField(String field) {
		return field.toLowerCase();
	}

	/**
	 * 	聚合
	 * @param searchBuilder
	 * @param page
	 * @param types
	 * @return
	 */
	public static List<Object> findAllPageAggResult(SearchRequestBuilder searchBuilder ,Pageable page ,String types) {
		List<Object> dataBeanList = new ArrayList<Object>() ;
		if(searchBuilder != null) {
			searchBuilder.setTypes(types).setIndices(UKDataContext.SYSTEM_INDEX);
			SearchResponse response = searchBuilder.execute().actionGet();
			for(Object value : response.getAggregations()){
				if(value instanceof Terms){
					Terms agg = (Terms)value;
					if(agg.getBuckets()!=null && agg.getBuckets().size()>0){
						for (Terms.Bucket entry : agg.getBuckets()) {
							WebIMReport dataBean = new WebIMReport();
							dataBean.setData(entry.getKeyAsString());
							for (Aggregation temp : entry.getAggregations()) {
								if(temp.getName().equals("id")) {
									dataBean.setPvnums(entry.getDocCount()) ;
								}
								if(temp.getName().equals("ip")) {
									InternalCardinality agg2  = (InternalCardinality) temp ;
									dataBean.setIpnums(agg2.getValue()) ;
								}
							}
							dataBeanList.add(dataBean) ;
						}
					}
				}
			}
		}
		return dataBeanList;
	}
	/**
	 * 	首页，已邀请/已拒绝
	 * @param searchBuilder
	 * @param page
	 * @param types
	 * @return
	 */
	public static List<Object> findAllPageAggResultOnlineUser(SearchRequestBuilder searchBuilder ,Pageable page ,String types) {
		List<Object> dataBeanList = new ArrayList<Object>() ;
		if(searchBuilder != null) {
			searchBuilder.setTypes(types).setIndices(UKDataContext.SYSTEM_INDEX);
			SearchResponse response = searchBuilder.execute().actionGet();
			WebIMReport webIMReport = new WebIMReport();
			for(Object value : response.getAggregations()){
				if(value instanceof Terms){
					Terms agg = (Terms)value;
					if(agg.getBuckets()!=null && agg.getBuckets().size()>0){
						for (Terms.Bucket entry : agg.getBuckets()) {
							if(entry.getKeyAsString().equals("invite")) {
								webIMReport.setInviteusers((int)entry.getDocCount());
							}
							if(entry.getKeyAsString().equals("refuse")) {
								webIMReport.setRefuseusers((int)entry.getDocCount());
							}
						}
					}
				}
			}
			dataBeanList.add(webIMReport) ;
		}
		return dataBeanList;
	}
	public static SearchDataBean findAllPageAggResultChatMessage(SearchRequestBuilder searchBuilder ,Pageable page ,String types) {
		SearchDataBean searchDataBean = new SearchDataBean();
		List<String> dataBeanList = new ArrayList<String>() ;
		if(searchBuilder != null) {
			searchBuilder.setTypes(types).setIndices(UKDataContext.SYSTEM_INDEX);
			SearchResponse response = searchBuilder.execute().actionGet();
			for(Object value : response.getAggregations()){
				if(value instanceof Terms){
					Terms agg = (Terms)value;
					if(agg.getBuckets()!=null && agg.getBuckets().size()>0){
						for (Terms.Bucket entry : agg.getBuckets().subList(page.getPageNumber()*10,agg.getBuckets().size())) {
							for (Aggregation temp : entry.getAggregations()) {
								if(temp instanceof StringTerms) {
									StringTerms agg2  = (StringTerms) temp ;
									for (Terms.Bucket entry2 : agg2.getBuckets()) {
										dataBeanList.add(entry2.getKeyAsString());
									}
								}
							}
						}
					}
				}else if(value instanceof InternalCardinality) {
					InternalCardinality agg = (InternalCardinality)value;
					searchDataBean.setSize((int)agg.getValue());
				}
			}
			searchDataBean.setList(dataBeanList);
		}
		return searchDataBean;
	}
	/**
	 * 
	 * @param dataBean
	 */
	public static UKDataBean processDate(UKDataBean dataBean) {
		return dataBean;
	}
	public static PageImpl<UKDataBean> findAllPageMetadata(QueryBuilder query,Pageable page ,String types) {
		List<UKDataBean> dataBeanList = new ArrayList<UKDataBean>() ;
		SearchRequestBuilder searchBuilder = UKDataContext.getTemplet().getClient().prepareSearch(UKDataContext.SYSTEM_INDEX);
		if(!StringUtils.isBlank(types)) {
			searchBuilder.setTypes(types) ;
		}
		int start = page.getPageSize() * page.getPageNumber();
		searchBuilder.setFrom(start).setSize(page.getPageSize());
		if(page!=null && page.getSort()!=null) {
			Iterator<Order> iterator = page.getSort().iterator();
			while(iterator.hasNext()) {
				Order order = iterator.next() ;
				searchBuilder.addSort(new FieldSortBuilder(order.getProperty()).unmappedType(order.getProperty().equals("createtime")? "long" : "string").order( order.isDescending() ? SortOrder.DESC : SortOrder.ASC)) ;
			}
		}
		SearchResponse response = searchBuilder.setQuery(query).execute().actionGet();
		for(SearchHit hit : response.getHits().getHits()){
			UKDataBean temp = new UKDataBean() ;
			temp.setType(hit.getType());
			temp.setValues(hit.getSource());
			temp.setId(hit.getId());
			dataBeanList.add(processDate(temp)) ;
		}
		return new PageImpl<UKDataBean>(dataBeanList,page , (int)response.getHits().getTotalHits());
	}
	
	public static List<Object> findByContactsItemAggResult(SearchRequestBuilder searchBuilder ,Pageable page ,String types) {
		List<Object> dataBeanList = new ArrayList<Object>() ;
		if(searchBuilder != null) {
			searchBuilder.setTypes(types).setIndices(UKDataContext.SYSTEM_INDEX);
			SearchResponse response = searchBuilder.execute().actionGet();
			for(Object value : response.getAggregations()){
				if(value instanceof Terms){
					Terms agg = (Terms)value;
					if(agg.getBuckets()!=null && agg.getBuckets().size()>0){
						for (Terms.Bucket entry : agg.getBuckets()) {
							WebIMReport dataBean = new WebIMReport();
							dataBean.setData(entry.getKeyAsString());
							dataBeanList.add(dataBean) ;
						}
					}
				}
			}
		}
		return dataBeanList;
	}
	
	/**
	 *	 联系人 知识库首页 - 数据聚合
	 */
	public static PageImpl<UKDataBean> aggregationContacts(BoolQueryBuilder queryBuilder , String aggField, boolean loadRef , int p, int ps){
		ESDataExchangeImpl esDataExchange = UKDataContext.getContext().getBean(ESDataExchangeImpl.class);
		return esDataExchange.findAllPageAggResultContacts(queryBuilder , aggField ,  new PageRequest(p, ps , Sort.Direction.ASC, "createtime") , loadRef ,  UKDataContext.EsTable.UK_CONTACTS.toString()) ;
	}
	
	public static boolean isDate(String str,SimpleDateFormat formatter){
		if (!StringUtils.isBlank(str)) {
			formatter.setLenient(false);
			try {
				formatter.format(formatter.parse(str));
			} catch (Exception e) {
				return false;
			}
			return true;
		}
		return false;
	}
}
